<?php

$test_proxy_bin = "/home/luanjunyi/yhhd/py/taras/tools/test_proxy";
ob_implicit_flush();

require_once "../config.php";
require_once "../util.php";

if (isset($_GET['addr'])) {
  // Test single proxy
  $addr = trim($_GET['addr']);
  $port = trim($_GET['port']);
  $user = trim($_GET['user']);
  $passwd = trim($_GET['passwd']);


  if (!$port || !$user || !$passwd) {
    connect_db();
    $sql = "select * from proxy where addr = '$addr'";
    $ret = mysql_query($sql);
    if (!$ret) {
      echo("$sql failed: " . mysql_error());
    }

    $row = mysql_fetch_assoc($ret);
    if (!$ret) {
      die("$sql got " . mysql_num_rows($ret) . " rows");
    }
    $port = $row['port'];
    $user = $row['user'];
    $passwd = $row['password'];
  }

} else {
  die("no ip address specified");
}
$cmd = "{$test_proxy_bin} -c check-one -h $db_addr -i $addr -o $port -u $user -p $passwd 2>&1";

echo "<pre>";
system($cmd);
echo "</pre>";
?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="/lib/js/jquery.js"></script>
    <script type="text/javascript" src="/lib/js/jquery.form.js"></script>
    <script type="text/javascript" src="/lib/js/sprintf.js"></script>
    <link rel="stylesheet" type="text/css" href="/lib/css/reset.css" />
  
    <style type="text/css">
      input {display: block; margin-bottom: 6px;}
    </style>
  </head>

  <body>
    <form action="add-proxy.php" method="post">
      IP地址: <input type="text" name="addr" value="<?= $addr ?>" />
      端口: <input type="text" name="port" value="<?= $port ?>" />
      用户名: <input type="text" name="user" value="<?= $user ?>" />
      密码: <input type="text" name="passwd" value="<?= $passwd ?>" />
      <input type="submit" value="添加到数据库" />
    </form>
  </doby>
</html>
